import needLoging from './needLogin/needLogin.js'
import api from '@/common/reqlist/reqList.js'



const util = {
	subStr(begin,finish,str,ss = ''){
		if(str.length > finish){
			return str.slice(begin,finish)+ss
		}else{
			return str
		}
	},
	/**
	 * 遍历数组，查询是否存在
	 */
	// that:this,
	console(e) {
		console.log(e)
	},
	dateDiff(timestamp) {
		// 补全为13位
		var arrTimestamp = (timestamp + '').split('');
		for (var start = 0; start < 13; start++) {
			if (!arrTimestamp[start]) {
				arrTimestamp[start] = '0';
			}
		}
		timestamp = arrTimestamp.join('') * 1;

		var minute = 1000 * 60;
		var hour = minute * 60;
		var day = hour * 24;
		var halfamonth = day * 15;
		var month = day * 30;
		var now = new Date().getTime();
		var diffValue = now - timestamp;

		// 计算差异时间的量级
		var monthC = diffValue / month;
		var weekC = diffValue / (7 * day);
		var dayC = diffValue / day;
		var hourC = diffValue / hour;
		var minC = diffValue / minute;

		// 数值补0方法
		var zero = function(value) {
			if (value < 10) {
				return '0' + value;
			}
			return value;
		};

		// 使用

		// 超过1年，直接显示年月日
		return (function() {
			var date = new Date(timestamp);
			return date.getFullYear() + '年' + zero(date.getMonth() + 1) + '月' + zero(date.getDate()) + '日';
		})();
	},
	traverseArray(item, array) {
		let i = false
		array.forEach(e => {
			if (item == e) {
				i = true
			}
		})
		return i
	},
	/**
	 * 判断路径跳转是否需要登录
	 */
	navigateTo: async (url, data = '') => {
		console.log(url)
		const res = util.traverseArray(url, needLoging)
		if (res) {
			//需要登录才能放行
			const nickName = uni.getStorageSync('nickName') || '请先登录';
			const token = uni.getStorageSync('token')||'';
			if (token != '') {
				console.log("可以跳转,token = "+token)
				
				uni.navigateTo({
					url: url + data,
					fail(e) {
						console.log(e)
					}
				})
			} else {
				console.log("不可以跳转,token = "+token)
				uni.clearStorage();
				uni.showToast({
					title: '请先登录',
					icon: 'none'
				})
			}
		} else {
			uni.navigateTo({
				url: url
			})
		}
	}
}

export default util
